//
//  ObservableConvertibleType+Rx.swift
//  TestSwift
//
//  Created by Song on 2018/9/17.
//  Copyright © 2018年 Song. All rights reserved.
//

import RxSwift

extension ObservableConvertibleType where E: Equatable {
    /// 判断初始值和当前值是否相同
    ///
    /// - Returns: （value：当前值，isEqualOriginValue：是否相同）
    func isEqualOriginValue() -> Observable<(value: E, isEqualOriginValue: Bool)> {
        return self.asObservable()
            .scan(nil){ acum, x -> (origin: E, current: E)? in
                if let acum = acum {
                    return (origin: acum.origin, current: x)
                } else {
                    return (origin: x, current: x)
                }
            }
            .map { ($0!.current, isEqualOriginValue: $0!.origin == $0!.current) }
    }
}

extension ObservableConvertibleType {
    /// 重复执行某个序列
    ///
    /// - Parameter notifier: 触发该原序列发送的序列
    /// - Returns: 原序列
    func repeatWhen<O: ObservableType>(_ notifier: O) -> Observable<E> {
        return notifier.map { _ in }
            .startWith(())
            .flatMap { () -> Observable<E> in
                self.asObservable()
        }
    }
}
